/**
 *
 */

package com.moogene.android.engine.mmi.widget;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.moogene.android.engine.mmi.Layer;

public abstract class Widget extends Layer {

	private OnClickListener onClickListener = null;
	private OnPressedListener onPressedListener = null;
	private OnCancelListener onCancelListener = null;
	private boolean isClickSuccess;

	Bitmap background = null;

	public Widget(int x, int y, int width, int height) {
		super(x, y, width, height);
	}

	/**
     * @param width
     * @param height
     */
    public Widget(int width, int height) {
        super(width, height);
    }

    public Widget() {
		super();
	}

	public void setBackground(Bitmap img){
	    background = img;
	}


	public void setOnClickListener(OnClickListener l) {
		onClickListener = l;
	}

	public void setOnPressedListener(OnPressedListener l) {
		onPressedListener = l;
	}

	public void setOnCancelListener(OnCancelListener l) {
		onCancelListener = l;
	}

	@Override
	protected boolean canFoucs() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.moogene.android.engine.mmi.Layer#update()
	 */
	@Override
	public void update() {
		if (isRealeased()) {
			if (onClickListener != null) {
				onClickListener.onClick(this);
			}
			isClickSuccess = true;
		}
		if (isEndHold()) {
			if(onCancelListener != null && !isClickSuccess){
				onCancelListener.onCancel(this);
			}
			isClickSuccess = false;
		}
		if (isStartHold() && onPressedListener != null) {
			onPressedListener.onPressed(this);
		}

	}



	protected void onDraw(Canvas canvas) {

	}

	/**
	 * Interface definition for a callback to be invoked when a view is clicked.
	 */
	public interface OnClickListener {
		/**
		 * Called when a view has been clicked.
		 *
		 * @param v
		 *            The view that was clicked.
		 */
		void onClick(Widget v);
	}

	/**
	 * Interface definition for a callback to be invoked when a view is clicked.
	 */
	public interface OnPressedListener {
		/**
		 * Called when a view has been clicked.
		 *
		 * @param v
		 *            The view that was clicked.
		 */
		void onPressed(Widget v);
	}

	/**
	 * Interface definition for a callback to be invoked when a view's action is
	 * cancelled.
	 */
	public interface OnCancelListener {
		/**
		 * Called when a view has been clicked.
		 *
		 * @param v
		 *            The view that was clicked.
		 */
		void onCancel(Widget v);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.moogene.android.engine.mmi.Layer#draw(android.graphics.Canvas)
	 */
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		if(background != null){
		    canvas.drawBitmap(background, null, getDstRect(), null);
		}
		onDraw(canvas);
	}
}
